package com.lakeworks.service.impl;

import com.lakeworks.common.core.domain.model.LoginUser;
import com.lakeworks.common.exception.ServiceException;
import com.lakeworks.common.exception.response.ResponseException;
import com.lakeworks.domain.SysChannel;
import com.lakeworks.domain.SysChannelAuth;
import com.lakeworks.domain.status.PostRecord;
import com.lakeworks.domain.status.PostWrap;
import com.lakeworks.domain.status.StatusRecord;
import com.lakeworks.mapper.PostWrapMapper;
import com.lakeworks.mapper.SysChannelMapper;
import com.lakeworks.service.IPostWrapService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.lakeworks.common.enums.ResponseEnum.STATUS_UPDATE_ERROR;
import static com.lakeworks.common.utils.SecurityUtils.getLoginUser;
import static com.lakeworks.common.utils.SecurityUtils.getPrincipal;

/**
 * 包装记录Service业务层处理
 *
 * @author lake
 * @date 2023-02-23
 */
@Service
public class PostWrapServiceImpl implements IPostWrapService {
    @Resource
    private PostWrapMapper postWrapMapper;

    @Resource
    private SysChannelMapper sysChannelMapper;

    /**
     * 产品操作记录保存
     */
    @Override
    public void savePostRecord(String table, Long oprProductId, Long userId, String userName) {
        if (postWrapMapper.savePostRecord(table, oprProductId, userId, userName,1) == 0)
            throw new RuntimeException("记录保存失败！");
    }

    /**
     * 状态保存（入厂单、产品、出厂单）
     */
    @Override
    public void saveStatusRecord(String table, String field, Long objId, String status, Long userId, String userName) {
        if (postWrapMapper.saveStatusRecord(table, field, objId, status, userId, userName,1) == 0)
            throw new ResponseException(STATUS_UPDATE_ERROR);
    }

    /**
     * 状态保存（入厂单、产品、出厂单） 渠道用
     */
    @Override
    public void saveStatusRecord(String table, String field, Long objId, String status) {
        SysChannel sysChannel = new SysChannel();
        if (getPrincipal() instanceof SysChannelAuth)
            sysChannel = sysChannelMapper.selectSysChannelById(((SysChannelAuth) getPrincipal()).getChannelId());

        if (postWrapMapper.saveStatusRecord(table, field, objId, status, sysChannel.getId(), sysChannel.getName(),2) == 0)
            throw new ResponseException(STATUS_UPDATE_ERROR);
    }

    @Override
    public void commonStatusRecord(String table, String field, Long objId, String status) {
        SysChannel sysChannel = new SysChannel();
        LoginUser loginUser = new LoginUser();
        try {
            if (getPrincipal() instanceof SysChannelAuth) {
                sysChannel = sysChannelMapper.selectSysChannelById(((SysChannelAuth) getPrincipal()).getChannelId());
                if (postWrapMapper.saveStatusRecord(table, field, objId, status, sysChannel.getId(), sysChannel.getName(),2) == 0)
                    throw new ResponseException(STATUS_UPDATE_ERROR);
            } else {
                loginUser = getLoginUser();
                if (postWrapMapper.saveStatusRecord(table, field, objId, status, loginUser.getUserId(), loginUser.getUsername(),1) == 0)
                    throw new ResponseException(STATUS_UPDATE_ERROR);
            }
        } catch (ServiceException e) {
            postWrapMapper.saveStatusRecord(table, field, objId, status, 9999l, "robot",2);
        }

    }

    @Override
    public void commonPostRecord(String table, Long oprProductId) {
        SysChannel sysChannel = new SysChannel();
        LoginUser loginUser = new LoginUser();
        try {
            if (getPrincipal() instanceof SysChannelAuth) {
                sysChannel = sysChannelMapper.selectSysChannelById(((SysChannelAuth) getPrincipal()).getChannelId());
                if (postWrapMapper.savePostRecord(table, oprProductId, sysChannel.getId(), sysChannel.getName(),2) == 0)
                    throw new RuntimeException("记录保存失败！");
            } else {
                loginUser = getLoginUser();
                if (postWrapMapper.savePostRecord(table, oprProductId, loginUser.getUserId(), loginUser.getUsername(),1) == 0)
                    throw new RuntimeException("记录保存失败！");
            }
        } catch (ServiceException e){
            postWrapMapper.savePostRecord(table, oprProductId, 9999l, "robot",2);
        }

    }

    @Override
    public PostRecord getPostRecord(String table, Long oprProductId) {
        return postWrapMapper.queryPostRecord(table, oprProductId);
    }

    @Override
    public StatusRecord getStatusRecord(String table, String field, Long objId, String status) {
        return postWrapMapper.queryStatusRecord(table, field, objId, status);
    }

    /**
     * 查询包装记录
     *
     * @param id 包装记录主键
     * @return 包装记录
     */
    @Override
    public PostWrap selectPostWrapById(Long id) {
        return postWrapMapper.selectPostWrapById(id);
    }

    /**
     * 查询包装记录列表
     *
     * @param postWrap 包装记录
     * @return 包装记录
     */
    @Override
    public List<PostWrap> selectPostWrapList(PostWrap postWrap) {
        return postWrapMapper.selectPostWrapList(postWrap);
    }

    /**
     * 新增包装记录
     *
     * @param postWrap 包装记录
     * @return 结果
     */
    @Override
    public int insertPostWrap(PostWrap postWrap) {
        return postWrapMapper.insertPostWrap(postWrap);
    }

    /**
     * 修改包装记录
     *
     * @param postWrap 包装记录
     * @return 结果
     */
    @Override
    public int updatePostWrap(PostWrap postWrap) {
        return postWrapMapper.updatePostWrap(postWrap);
    }

    /**
     * 批量删除包装记录
     *
     * @param ids 需要删除的包装记录主键
     * @return 结果
     */
    @Override
    public int deletePostWrapByIds(Long[] ids) {
        return postWrapMapper.deletePostWrapByIds(ids);
    }

    /**
     * 删除包装记录信息
     *
     * @param id 包装记录主键
     * @return 结果
     */
    @Override
    public int deletePostWrapById(Long id) {
        return postWrapMapper.deletePostWrapById(id);
    }
}
